package graph;

import java.util.ArrayList;

public class Node {
	private ArrayList<Edge> outgoingEdges = new ArrayList<Edge>();
	private ArrayList<Edge> incomingEdges = new ArrayList<Edge>();
	private String name = "";
	private int earliest = -1;
	private int latest = -1;

	public Node(String name) {
		this.name = name;
	}

	public int getCostTo(Node node) {
		for (Edge edge : outgoingEdges) {
			if (edge.getEnd().getName().equals(node.getName())) {
				return edge.getCost();
			}
		}
		return -1;
	}

	public ArrayList<Edge> getOutgoing() {
		return outgoingEdges;
	}

	public boolean checkIncoming() {
		return incomingEdges.isEmpty();
	}

	public ArrayList<Edge> getIncoming() {
		return incomingEdges;
	}

	public void setEarliest(int earliest) {
		this.earliest = earliest;
	}

	public void setLatest(int latest) {
		this.latest = latest;
	}

	public int getEarliest() {
		return earliest;
	}

	public int getLatest() {
		return latest;
	}

	public void addIncomingNode(Node beginning, Node node, int cost) {
		incomingEdges.add(new Edge(beginning, node, cost));
	}

	public void addEdge(Node beginning, Node to, int cost) {
		outgoingEdges.add(new Edge(this, to, cost));
	}

	public void removeEdgeTo(Node to) {
		for (int i = 0; i < outgoingEdges.size(); i++) {
			if (outgoingEdges.get(i).getEnd().equals(to)) {
				outgoingEdges.remove(i);
			}
		}
	}

	public void removeEdgeFrom(Node from) {
		for (int i = 0; i < incomingEdges.size(); i++) {
			if (incomingEdges.get(i).getBeginning().equals(from)) {
				incomingEdges.remove(i);
			}
		}
	}

	public Edge getEdgeTo(Node node) {
		for (Edge edge : outgoingEdges) {
			if (edge.getEnd().getName().equals(node.getName())) {
				return edge;
			}
		}
		return null;
	}

	public String getName() {
		return name;
	}

	public String toString() {
		return name + "(" + earliest + "-" + latest + ")";
	}

	// returning a list
	// list contains nodes which can be accessed from the edges
	// "ascending nodes"
	public ArrayList<Node> getNextNeighbours() {
		ArrayList<Node> neighbours = new ArrayList<Node>();
		for (Edge edge : outgoingEdges) {
			neighbours.add(edge.getEnd());
		}
		return neighbours;
	}
}
